package com.zp.qualitypractice.list.chain;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by izp on 2017/9/6.
 */

public abstract class ChainRecyclerAdapter<T> extends RecyclerView.Adapter<ChainViewHolder> {
    private Context mCtx;
    private List<T> mValueList;
    private boolean mIsEnableLoadMore;

    public ChainRecyclerAdapter(Context mCtx, List<T> mValueList) {
        this.mCtx = mCtx;
        this.mValueList = mValueList;
    }
    public ChainRecyclerAdapter(Context mCtx, List<T> mValueList,boolean enableLoadMore) {
        this(mCtx,mValueList);
        this.mIsEnableLoadMore = enableLoadMore;
    }

    @Override
    public ChainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mCtx).inflate(viewType, parent, false);
        return ChainViewHolder.createViewHolder(mCtx, itemView);
    }

    @Override
    public void onBindViewHolder(ChainViewHolder holder, int position) {
        convert(holder, getValue(position), getItemViewType(position), position);
    }

    @Override
    public int getItemCount() {
        if (isEnabelLoadMore()){
            return mValueList.size()+1;
        }
        return mValueList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return getLayoutAsViewType(getValue(position), position);
    }

    public List<T> getValueList() {
        return mValueList;
    }

    public T getValue(int position) {
        if (isEnabelLoadMore()){
            if (position==mValueList.size()){
                return null;
            }else {
                return getValueList().get(position);
            }
        }else {
            return getValueList().get(position);
        }
    }
    public boolean isEnabelLoadMore(){
        return mIsEnableLoadMore;
    }
    public void setLoadMore(boolean enableLoadMore){
        mIsEnableLoadMore = enableLoadMore;
    }

    public abstract void convert(ChainViewHolder holder, T t, int layoutType, int position);


    public abstract @LayoutRes int getLayoutAsViewType(T t, int position);
}
